home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5700 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news.compuserve.com!newsmaster
  2. From: johnb@pivotal-dm.ccmail.compuserve.com (John Bain)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Borland C++ 4.5 : delete [] operator - what's going on here?
  5. Date: Tue, 06 Feb 1996 12:21:14 GMT
  6. Organization: Pivotal Technologies
  7. Message-ID: <31171e2d.7861133@dub-news-svc-3.compuserve.com>
  8. References: <4espam$ddf@rks1.urz.tu-dresden.de>
  9. NNTP-Posting-Host: dd62-138.compuserve.com
  10. X-Newsreader: Forte Agent .99c/16.141
  11.  
  12. Hoang Minh Son <hoang@eatns1.et.tu-dresden.de> wrote:
  13.  
  14. >template<class T>
  15. >TMatrix<T>::TMatrix(size_e m, size_t n) : nrow(m), ncol(n)
  16. >{    
  17. >        elem = new T[m*n+1];  //  a dummy space for efficient use 
  18. >        pcol = new T*[n+1];   //  of 1-based indexing
  19.  
  20. I.e. pcol points to an array of n+1 T*'s, so pcol[x] is valid
  21. for 0<=x<=n.
  22.  
  23. >            
  24. >    if (n > 0)
  25. >    {
  26. >        pcol[1] = elem;
  27. >        for (int i=1; i <= n; i++)
  28. >            pcol[i+1] = pcol[i] + m;
  29. >    }
  30. >}
  31.  
  32. The for loop's last write is to pcol[n+1] - outside the allocated range.
  33.  
  34. You probably want
  35.  
  36.   for (int i=1; i < n; i++)
  37.      pcol[i+1] = pcol[i] + m;
  38.  
  39. or, preferably (IMHO)
  40.  
  41.   for (int i=2; i <= n; i++)
  42.      pcol[i] = pcol[i-1] + m;
  43.  
  44. Regards,
  45.  
  46. John
  47. -----------------------------------------------------------------
  48. John Bain                  
  49. johnb@pivotal-dm.ccmail.compuserve.com 
  50. -----------------------------------------------------------------
  51.  
  52.